var app = getApp();
Page({
  data: {
    core: [],
    server: app._server,
    help_status: false,
    notice: '',
    isCanDraw: false,
    isLoginShow: false,
  },
  onPullDownRefresh: function() {
    this.GetAllUser();
    wx.stopPullDownRefresh();
  },
  GetAllUser: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetSpName',
        Sp_Name: that.data.spname
      },
      success: (res) => {
        console.log('res', res)
        var cores = res.data.rows;
        for (var i = 0; i < cores.length; i++) {
          cores[i].Remarks = cores[i].Remarks.replace(/<br\/>/g, "\r\n");
        }
        that.setData({
          core: cores
        })
        wx.hideLoading()
      },
      fail: function(res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    });
  },
  onLoad: function(options) {
    let loginData = wx.getStorageSync('login')
    if (!loginData) {
      this.setData({
        isLoginShow: true
      })
      return
    }
    this.setData({
      spname: options.id,
      user: app._user
    })
    this.GetAllUser()
  },
  openDetails: function(e) {
    var index = !isNaN(e) ? e : parseInt(e.currentTarget.dataset.index),
      data = {};
    data['core[' + index + '].display'] = !this.data.core[index].display,
      data['core[' + index + '].Gray'] = !this.data.core[index].Gray;
    this.setData(data);
  },
  openEvent: function(e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var qi = e.currentTarget.dataset.qi;
    var str = id + ',' + name + ',' + qi;
    wx.navigateTo({
      url: '/pages/baoming/baoming?ID=' + str
    })
  },
  openEvent_hz: function(e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var qi = e.currentTarget.dataset.qi;
    var date = new Date(e.currentTarget.dataset.end).getTime() - new Date(e.currentTarget.dataset.star).getTime();
    var days = Math.floor(date / (24 * 3600 * 1000)) + 1;
    var str = id + ',' + name + ',' + qi + ',' + days;
    wx.navigateTo({
      url: '/pages/baoming/Foodbaoming?ID=' + str
    })
  },
  tapHelp: function(e) {
    if (e.target.id == 'help') {
      this.hideHelp();
    }
  },
  showHelp: function(e) {
    let index = e.currentTarget.dataset.index
    let notice = this.data.core[index].Notice
    if (notice == '' || !notice) {
      wx.showModal({
        title: '提示',
        content: '该培训无报名通知',
        showCancel: false,
        confirmColor: '#cc0000'
      })
    } else {
      this.setData({
        'help_status': true,
        notice: notice
      })
    }
  },
  hideHelp: function(e) {
    this.setData({
      'help_status': false
    });
  },
  createShareImage() {
    this.setData({
      isCanDraw: !this.data.isCanDraw
    })
  },
  clickScale(e) {
    let imgSrc = e.target.dataset.index
    // 预览图片
    wx.previewImage({
      urls: [imgSrc],
      current: imgSrc,
    })
  },
})